$(function () {
    $("#jqGrid").Grid({
        url: '../user/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '会员名称', name: 'username', index: 'username', width: 60},
            {label: '会员密码', name: 'password', index: 'password', hidden: true},
            {label: '性别', name: 'gender', index: 'gender', width: 25,
                formatter: function (value) {return transGender(value);}
            },
            {label: '出生日期', name: 'birthday',hidden: true, index: 'birthday',  width: 80,
                formatter: function (value) {return transDate(value);}
             },
            {label: '身份证', name: 'idcard', index: 'idcard', width: 80},
            {label: '公司', name: 'brandname', index: 'brandname', width: 60},
            {label: '邮箱', name: 'email', index: 'email', width: 60},
            {label: '职位', name: 'job', index: 'job', width: 40},
            {label: '注册时间', name: 'registerTime', index: 'register_time', width: 80,
                formatter: function (value) {return transDate(value);}
            },
            {label: '最后登录时间', name: 'lastLoginTime', index: 'last_login_time',hidden: true, width: 80,
                formatter: function (value) {return transDate(value);}
            },
            {label: '最后登录Ip', name: 'lastLoginIp', index: 'last_login_ip', hidden: true},
            {label: '会员等级', name: 'levelName', width: 40},
            {label: '微信名', name: 'nickname', index: 'nickname', hidden: true, width: 80},
            {label: '手机号码', name: 'mobile', index: 'mobile', width:60},
            {label: '注册Ip', name: 'registerIp', index: 'register_ip', hidden: true},
            {label: '分销类型', name: 'authtype', index: 'authtype',width: 40, formatter: function (value) {
                    if(value==0)//认证类型0普通1申请消费者2经营3一级
                        return '消费者';
                    else if(value==1)
                        return '一级分销申请';
                    else if(value==2)
                        return '经营者';
                    else if(value==3)
                        return '一级分销';
                    else
                        return '';
                }
            },
            {label: '头像', name: 'avatar', index: 'avatar', width: 40,
                formatter: function (value) {return transImg(value);}
            },
            {label: '微信Id', name: 'weixinOpenid', index: 'weixin_openid', width: 80, hidden: true},
            {name:'已经存在的列名称', label:'操作', width: 180,formatter:function(cellvalue, options, row){
                    if(row.authtype==1)//认证类型0普通1申请消费者2经营3一级
                        return '<a href="#" onclick="vm.applyFirstSales(\''+row.id+'\')">审核一级分销商</a><a href="#" style="margin-left: 20px" onclick="vm.opebmangetag(\''+row.id+'\')">用户标签管理</a>';
                    else if(row.authtype==0)
                        return '<a href="#" onclick="vm.openSecondSales(\''+row.id+'\')">升级经营者</a><a href="#" style="margin-left: 20px" onclick="vm.openFirstSales(\''+row.id+'\')">邀请一级分销商</a><a href="#" style="margin-left: 20px" onclick="vm.opebmangetag(\''+row.id+'\')">用户标签管理</a>';
                    else if(row.authtype==2)
                        return '<a href="#" onclick="vm.openFirstSales(\''+row.id+'\')">邀请一级分销商</a><a href="#" style="margin-left: 20px" onclick="vm.opebmangetag(\''+row.id+'\')">用户标签管理</a>';
                    else
                        return '';
                }
            }]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        user: {
            gender: 1
        },
        ruleValidate: {
            username: [
                {required: true, message: '会员名称不能为空', trigger: 'blur'}
            ],
            mobile: [
                {required: true, message: '手机号不能为空', trigger: 'blur'},
                {type: 'tel', message: '手机号格式不正确', trigger: 'blur'}
            ]
        },
        q: {
            username: ''
        },
        userLevels: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.user = {gender: '1'};
            vm.userLevels = [];

            this.getUserLevels();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
            this.getUserLevels();
        },
        saveOrUpdate: function (event) {
            var url = vm.user.id == null ? "../user/save" : "../user/update";

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.user),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../user/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });

            });
        },
        exportUser: function () {
            exportFile('#rrapp', '../user/export', {'username': vm.q.username});
        },
        coupon: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            openWindow({
                title: '优惠券',
                type: 2,
                content: '../shop/usercoupon.html?userId=' + id
            })
        },
        address: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            openWindow({
                title: '收获地址',
                type: 2,
                content: '../shop/address.html?userId=' + id
            })
        },
        shopCart: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }

        },
        getInfo: function (id) {
            Ajax.request({
                url: "../user/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.user = r.user;
                }
            });
        },
        /**
         * 获取会员级别
         */
        getUserLevels: function () {
            Ajax.request({
                url: "../userlevel/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.userLevels = r.list;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'username': vm.q.username},
                page: page,
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        openSecondSales: function (id) {
            vm.user.id = id;
            vm.user.authtype = 2;
            vm.user.isshare = 1;
            confirm('确定升级？', function () {
                Ajax.request({
                    url: "../user/update",
                    params: JSON.stringify(vm.user),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        openFirstSales: function (id) {
            vm.user.id = id;
            vm.user.authtype = 100;
            confirm('确定邀请？', function () {
                Ajax.request({
                    url: "../user/update",
                    params: JSON.stringify(vm.user),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        applyFirstSales: function (id) {
            vm.user.id = id;
            vm.user.authtype = 3;
            vm.user.isshare =1 ;
            confirm('确定审核通过？', function () {
                Ajax.request({
                    url: "../user/update",
                    params: JSON.stringify(vm.user),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        opebmangetag: function (userid) {
            openWindow({
                type: 2,
                title: '页签管理',
                content: '../shop/usertag.html?userid='+userid
            })
        }
    }
});